package marksys.free.com.marksys.utils;

import android.content.Context;
import android.util.SparseArray;
import android.widget.Toast;

import java.util.concurrent.Executor;

import javax.inject.Inject;

import marksys.free.com.marksys.nettool.common.Resource;
import marksys.free.com.marksys.nettool.Constant;
import marksys.free.com.marksys.ui.user.LoginActivity;

/**
 * 暂时先这样
 *
 * @author yawei
 * @data on 2018/7/27 下午2:52
 * @email zyawei@live.com
 */

public class HttpErrorProcess {

    private final Executor appExecutors;
    private final Context application;
    private SparseArray<Runnable> runnableSparseArray;


    @Inject
    HttpErrorProcess(AppExecutors appExecutors, Context application) {
        this.appExecutors = appExecutors.mainThread();
        this.application = application;
    }


    public HttpErrorProcess(Executor appExecutors, Context context) {
        this.appExecutors = appExecutors;
        this.application = context;
    }


    public HttpErrorProcess register(int code, Runnable runnable) {
        if (null == runnableSparseArray) {
            runnableSparseArray = new SparseArray<>(1);
        }
        runnableSparseArray.append(code, runnable);
        return this;
    }

    public void process(int code, String message) {

        Runnable runnable = null;
        if (runnableSparseArray != null) {
            runnable = runnableSparseArray.get(code);
        }
        if (runnable == null) {
            switch (code) {
                case 0:
                    return;
                case 3001:
                    runnable = () -> {
                        // TODO: 2018/8/23
                        Toast.makeText(application, "请重新登陆..", Toast.LENGTH_SHORT).show();

                        SharedPreferencesUtils.removeSync(Constant.UserToken);

                        ActivityManager.getInstance().starWithClearTask(LoginActivity.class, null);
                        /*Intent dialogIntent = new Intent(application, LoginActivity.class);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
                        application.startActivity(dialogIntent);*/
                    };
                    break;
                case 3003:
                    runnable = () -> Toast.makeText(application, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
                    break;
                case 4001:
                    runnable = () -> Toast.makeText(application, "参数错误", Toast.LENGTH_SHORT).show();
                    break;
                case 5001:
                    runnable = () -> Toast.makeText(application, "服务器发生错误", Toast.LENGTH_SHORT).show();
                    break;
                case 5002:
                    runnable = () -> Toast.makeText(application, "数据保存失败", Toast.LENGTH_SHORT).show();
                    break;
                case 5003:
                    runnable = () -> Toast.makeText(application, "数据删除失败", Toast.LENGTH_SHORT).show();
                    break;
                case 999:
                    runnable = () -> Toast.makeText(application, "网络异常 :" + message, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    runnable = () -> Toast.makeText(application, "未知错误 : " + message, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
        appExecutors.execute(runnable);
    }

    public void process(Resource resource) {
        process(resource.errorCode, resource.errorMessage);
    }
}
